//
//  LYTuiJianCellTableViewCell.m
//  开心一刻
//
//  Created by lanou on 16/4/7.
//  Copyright © 2016年 刘洋. All rights reserved.
//

#import "LYTuiJianCellTableViewCell.h"
#import "UIImageView+WebCache.h"

@interface LYTuiJianCellTableViewCell ()

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *height;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *shenPIngHeight;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *shenPingName;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *shenPIngWidth;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *moviewHeight;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topImage;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topTextHeight;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *typeHeight;
@property (nonatomic,assign)BOOL ioHa;

@end

@implementation LYTuiJianCellTableViewCell

- (void)awakeFromNib {
    // Initialization code
}

- (void)cellGetData : (LYTuiJianModel *)model
{
    _topHeadPhoto.layer.masksToBounds = YES;
    _topHeadPhoto.layer.cornerRadius = 20;
    
   
    if (model.avatar_url != nil)
    {
        _height.constant = 90;
        _pingLunPhoto.layer.masksToBounds = YES;
        _pingLunPhoto.layer.cornerRadius = 20;
        [_pingLunPhoto sd_setImageWithURL:[NSURL URLWithString:model.avatar_url]];
        _pingLuanName.text = model.user_name;
        _DownDesc.text = model.text;
   
    }
    else
    {
        _height.constant = 0;
        _shenPIngHeight.constant = 0;
        _shenPingName.constant = 0;
     
    }
    
    if (model.photoArray.count != 0)
    {
        // 多张图片
        for (int i = 0; i < model.photoArray.count; i++)
        {
            UIImageView * imageVIew = [[UIImageView alloc]initWithFrame:CGRectMake( ((KedWidth - 20 - 80 * 4) / 3) * (i %4) + 80 * (i % 4), (80 * model.height / model.width) * (i / 4) , 80, 80 * model.height / model.width)];
            imageVIew.userInteractionEnabled = YES;
            [imageVIew sd_setImageWithURL:[NSURL URLWithString:model.photoArray[i]]];
             [_playView addSubview:imageVIew];
            UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(aaa:)];
               [imageVIew addGestureRecognizer:tap];
        }
        _moviewHeight.constant = (int)(model.photoArray.count / 4 + 1) *  (80 * model.height / model.width);
        _topHeadPhoto.layer.masksToBounds = YES;
        _topHeadPhoto.layer.cornerRadius = 20;
        [_topHeadPhoto sd_setImageWithURL:[NSURL URLWithString:model.zuozhe]];
        _topName.text = model.name;
        _TopDes.text = model.title;
        _typeLable.layer.borderColor = [UIColor purpleColor].CGColor;
        _typeLable.layer.borderWidth = 2;
        _typeLable.layer.cornerRadius = 10;
        _typeLable.layer.masksToBounds = YES;
        _typeLable.text = model.category_name;
        
    }
    else
    {
        if (model.isMovie == YES)
        {
         
            [_topHeadPhoto sd_setImageWithURL:[NSURL URLWithString:model.zuozhe]];
            _topName.text = model.name;
            _TopDes.text = model.title;
            _typeLable.layer.borderColor = [UIColor purpleColor].CGColor;
            _typeLable.layer.borderWidth = 2;
            _typeLable.layer.cornerRadius = 10;
            _typeLable.layer.masksToBounds = YES;
            _typeLable.text = model.category_name;
            if (model.height != 0)
            {
                _moviewHeight.constant = (model.height * (KedWidth - 20))/ model.width;
                
                UIImageView * myimageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, KedWidth - 20, (model.height * (KedWidth - 20))/ model.width)];
                
                myimageView.userInteractionEnabled = YES;
                _playView.userInteractionEnabled = YES;
                [_playView addSubview: myimageView];
                [myimageView sd_setImageWithURL:[NSURL URLWithString:model.coverImage]];
                
                _button = [UIButton buttonWithType:UIButtonTypeCustom];
                _button.frame = CGRectMake((KedWidth - 60 ) / 2,((model.height * (KedWidth - 20))/ model.width) / 2, 40, 40);
                
    
                [_button setImage:[UIImage imageNamed:@"stop"] forState:UIControlStateNormal];
                [myimageView addSubview:_button];

                
            }
            else
            {
                
                _moviewHeight.constant = 0;
                _topTextHeight.constant = 150;
                
                
            }
         
        }
        else
        {
            [_topHeadPhoto sd_setImageWithURL:[NSURL URLWithString:model.zuozhe]];
           
            _topName.text = model.name;
            _TopDes.text = model.title;
            _typeLable.layer.borderColor = [UIColor purpleColor].CGColor;
            _typeLable.layer.borderWidth = 2;
            _typeLable.layer.cornerRadius = 10;
            _typeLable.layer.masksToBounds = YES;
            _typeLable.text = model.category_name;

            _moviewHeight.constant = ((KedWidth - 20) *model.height) / model.width;
            UIWebView * webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, KedWidth - 20, ((KedWidth - 20) *model.height) / model.width)];
            
            NSString * str = [model.url substringWithRange:NSMakeRange(21, 1)];
            if ([str isEqualToString:@"l"])
            {
                [_playView addSubview:webView];
                [ webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:model.url]]];
                
            }
            else
            {
                UIImageView * view = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, KedWidth-20, ((KedWidth - 20) *model.height) / model.width)];
                [view sd_setImageWithURL:[NSURL URLWithString:model.url]];
                [_playView addSubview:view];
            }
            
            
        }
        
        
        
        
    }
    
    
    
 
    
}
+(CGFloat)RowHeight : (LYTuiJianModel * )model
{
    if (model.height != 0 && model.avatar_url != nil)
    {
        if (model.photoArray.count != 0)
        {
            return (int)(model.photoArray.count / 4 + 1) *  (80 * model.height / model.width) +40 + 30 + 27 +40 + 80 +103;
        }
        if (model.isMovie == NO)
        {
            return 40 + 30 +((KedWidth - 20) *model.height) / model.width + 80;
            
            
            
        }
    return (model.height * (KedWidth - 20))/ model.width +40 + 30 + 27 +40 + 80 +103;
        
    }
else if (model.height == 0 && model.avatar_url != 0)
   {
       return 40+ 150 + 27 + 40 +80 +103;
   }
    
    else if (model.height !=0 && model.avatar_url == nil)
    {
        if (model.photoArray.count != 0)
        {
            return (int)(model.photoArray.count / 4 + 1) *  (80 * model.height / model.width) + 40 + 30 + 27 +63;
        }
        if (model.isMovie == NO)
        {
            return 40 + 30 +((KedWidth - 20) *model.height) / model.width +80;
        }

        return (model.height * (KedWidth - 20))/ model.width +40 + 30 + 27 +73 ;
    }
    
   else
   {
       return 40 +30 +150 + 65;
   }
}
#pragma mark --- 放大图片
- (void)aaa:(UITapGestureRecognizer *)sender
{
    UIImageView * view1 = (UIImageView *)sender.view;
    UIImageView * my = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, KedWidth , KedHeight)];
    my.userInteractionEnabled = YES;
    UITapGestureRecognizer * myTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
    [my addGestureRecognizer:myTap];
     my.image = view1.image;
        [self.superview.superview.superview addSubview : my];
  
    
}

#pragma mark---删除
- (void)tap : (UITapGestureRecognizer *)tap
{
    UIView * view = tap.view;
    [view removeFromSuperview];
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
